/**
 * Date:2021年3月30日 上午10:31:49
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com.app.common.core;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;

/**
 * Description: TODO <br/>
 * Date:2021年3月30日 上午10:31:49 <br/>
 * 
 * @author xuejianxin
 */
@Component
public class MyTransactionManager extends JdbcTransactionManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void prepareTransactionalConnection(Connection conn, TransactionDefinition definition)
			throws SQLException {
		super.prepareTransactionalConnection(conn, definition);
		boolean readOnly = conn.isReadOnly();
		try (Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("select @@server_id , @@hostname ")) {
			if (rs.next()) {
				System.out.printf("prepareTransactionalConnection serverid=%s,hostname=%s,readOnly=%s\n",
						rs.getString(1), rs.getString(2), readOnly);
			}
		}

	}

	@Autowired
	@Override
	public void setDataSource(DataSource dataSource) {
		super.setDataSource(dataSource);
	}

}
